package com.i.template.service.impl;

import com.i.template.constant.ResponseStatusEnum;
import com.i.template.domain.dto.MailDTO;
import com.i.template.exception.BusinessException;
import com.i.template.service.IMailService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @author lewis
 * @type class
 * @description 邮件服务层 发送功能
 * @since 2024/7/25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MailServiceImpl implements IMailService {
    private final JavaMailSender mailSender;
    private final MailProperties mailProperties;
    @Override
    public void sendSimpleMail(MailDTO mailDTO) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(mailMessage.getFrom());
            List<String> to = mailDTO.getTo();
            mailMessage.setTo(to.toArray(String[]::new));
            mailMessage.setSubject(mailDTO.getSubject());
            mailMessage.setText(mailDTO.getText());
        } catch (Exception e){
            log.error("邮件发送失败:{}",e.getMessage());
            throw new BusinessException(ResponseStatusEnum.MAIL_SEND_FAILED.getMessage(),ResponseStatusEnum.MAIL_SEND_FAILED.getCode());
        }
    }

    @Override
    public void sendHtmlMail(MailDTO mailDTO) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
            messageHelper.setFrom(mailProperties.getUsername());
            List<String> to = mailDTO.getTo();
            messageHelper.setTo(to.toArray(new String[0]));
            messageHelper.setSubject(mailDTO.getSubject());
            messageHelper.setText(mailDTO.getText(),true);
            if(mailDTO.getAttachmentPath() != null){
                List<String> pathList = mailDTO.getAttachmentPath();
                pathList.forEach(attachmentPath->{
                    File file = new File(attachmentPath);
                    if(file.exists()) {
                        String fileName = file.getName();
                        FileSystemResource fsr = new FileSystemResource(file);
                        try {
                            messageHelper.addAttachment(fileName,fsr);
                        } catch (MessagingException e) {
                            log.error("邮件附件添加失败:{}",e.getMessage());
                            throw new BusinessException("邮件附件添加失败",ResponseStatusEnum.MAIL_SEND_FAILED.getCode());
                        }
                    }
                });
            }
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            log.error("邮件发送失败:{}",e.getMessage());
            throw new BusinessException(ResponseStatusEnum.MAIL_SEND_FAILED.getMessage(),ResponseStatusEnum.MAIL_SEND_FAILED.getCode());
        }
    }

}
